package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.Allocation;
import com.unionpay.upyzt.resp.AllocationResp;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AllocationExample {

    public AllocationExample(String payBalanceAcctId, String recvBalanceAcctId) {
        this.payBalanceAcctId = payBalanceAcctId;
        this.recvBalanceAcctId = recvBalanceAcctId;
    }

    protected String payBalanceAcctId;

    protected String recvBalanceAcctId;


    /**
     * 运行 Demos
     *
     * @param payBalanceAcctId  付款方电子账簿ID
     * @param recvBalanceAcctId 收款方电子账簿ID
     * @return 分账ID
     */
    public static String runDemos(String payBalanceAcctId, String recvBalanceAcctId) throws UpyztException {
        AllocationExample allocationExample = new AllocationExample(payBalanceAcctId, recvBalanceAcctId);

        // 分账
        AllocationResp allocationResp = allocationExample.create();

        // 分账订单查询（系统订单号）
        allocationResp = allocationExample.retrieveById(allocationResp.getAllocationId());

        // 分账订单查询（平台订单号）
        allocationResp = allocationExample.retrieveByOutOrderNo(allocationResp.getOutOrderNo());

        return allocationResp.getAllocationId();
    }

    /**
     * 分账
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AllocationResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("pay_balance_acct_id", this.payBalanceAcctId);

        List<Map<String, Object>> transferParams = new ArrayList<>();
        Map<String, Object> transferParam = new HashMap<>();
        transferParam.put("recv_balance_acct_id", this.recvBalanceAcctId);
        transferParam.put("amount", 1);
        transferParam.put("metadata", MockData.metadata());
        transferParam.put("extra", MockData.extra());
        transferParams.add(transferParam);
        params.put("transfer_params", transferParams);
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        params.put("remark", "用于 SDK 示例测试 ");

        System.out.println("------- 分账 -------");
        AllocationResp resp = Allocation.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 分账订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AllocationResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 分账订单查询（系统订单号） -------");
        AllocationResp resp = Allocation.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 分账订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AllocationResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 分账订单查询（平台订单号） -------");
        AllocationResp resp = Allocation.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
